package com.ziki.kwdj.biz;

import java.io.IOException;
import java.io.InputStream;
import java.util.Stack;

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.ziki.kwdj.model.ClassInfo;
import com.ziki.kwdj.model.Info;

public class JsoupParser {
    private static String CHARSET = "utf-8";

    public static Info parseInfo(InputStream content) {
        try {
            Info info = new Info();
            String html = IOUtils.toString(content, CHARSET);
            Document doc = Jsoup.parse(html);
            String infoID = doc.getElementById("alpenlicebe_c_progress_infoID").attr("value");
            String userID = doc.getElementById("alpenlicebe_c_progress_userID").attr("value");
            String sessionID = doc.getElementById("alpenlicebe_c_progress_sessionID").attr("value");
            String nowTime = doc.getElementById("nowTime").attr("value");
            info.setUserID(userID);
            info.setInfoID(infoID);
            info.setSessionID(sessionID);
            info.setNowTime(NumberUtils.toInt(nowTime));
            info.setCurrentClassMinutes(info.getNowTime() / 60);
            return info;
        } catch (IOException e) {
        }
        return null;
    }

    public static Info parseClassInfoList(InputStream content) {
        try {
            Info info = new Info();
            String html = IOUtils.toString(content, CHARSET);
            Document doc = Jsoup.parse(html);
            Elements links = doc.select(".ntab02").select("a");
            Stack<ClassInfo> classInfoStack = new Stack<ClassInfo>();
            info.setClassInfoStack(classInfoStack);
            for (Element link : links) {
                String title = link.attr("title");
                String style = link.attr("style");
                if (!"点击播放".equals(title) && !"color:blue!important;".equals(style)) {
                    String href = link.attr("href");
                    ClassInfo classInfo = new ClassInfo();
                    String text = link.parent().siblingElements().get(3).text();
                    text = text.replace("分钟", "");
                    int classMinute = NumberUtils.toInt(text);
                    String className = title;
                    String classId = href.substring(href.indexOf("infoID=") + 7);
                    classInfo.setClassID(classId);
                    classInfo.setClassMinute(classMinute);
                    classInfo.setClassName(className);
                    classInfoStack.add(classInfo);
                }
            }
            Element option = doc.select(".pagination").select("option").last();
            int maxPage = NumberUtils.toInt(option.attr("value"));
            info.setMaxPage(maxPage);
            return info;
        } catch (IOException e) {
        }
        return null;
    }
}
